# -*- coding: utf-8 -*-
#
from __future__ import division

import numpy
import sympy

from ..helpers import untangle, rd


class HammerStroud(object):
    '''
    Preston C. Hammer and Arthur H. Stroud,
    Numerical Integration Over Simplexes,
    Mathematical Tables and Other Aids to Computation,
    Vol. 10, No. 55 (Jul., 1956), pp. 137-139,
    <https://doi.org/10.2307/2002484>.
    '''
    def __init__(self, n, index, symbolic=False):
        frac = sympy.Rational if symbolic else lambda x, y: x/y
        sqrt = numpy.vectorize(sympy.sqrt) if symbolic else numpy.sqrt

        self.dim = n
        if index == '1a':
            self.degree = 2
            r = (n + 2 - sqrt(n+2)) / (n+1) / (n+2)
            s = (n + 2 + n*sqrt(n+2)) / (n+1) / (n+2)
            data = [
                (frac(1, n+1), rd(n+1, [(r, n), (s, 1)]))
                ]
        elif index == '1b':
            self.degree = 2
            r = (n + 2 + sqrt(n+2)) / (n+1) / (n+2)
            s = (n + 2 - n*sqrt(n+2)) / (n+1) / (n+2)
            data = [
                (frac(1, n+1), rd(n+1, [(r, n), (s, 1)]))
                ]
        else:
            assert index == '2'
            self.degree = 3

            B = -frac((n+1)**2, 4 * (n+2))
            C = frac((n+3)**2, 4 * (n+1) * (n+2))

            r = frac(1, n+1)
            s = frac(1, n+3)
            t = frac(3, n+3)

            data = [
                (B, [(n+1) * [r]]),
                (C, rd(n+1, [(t, 1), (s, n)])),
                ]

        self.bary, self.weights = untangle(data)
        self.points = self.bary[:, 1:]
        return
